-- NewTeamSettingView

local NewTeamSettingPanel = import(".NewTeamSettingPanel")

local disOFNameAndBg = 0 -- 目标名字与背景图的X差距
local targetnameBgContentSize = nil -- 目标初始底尺寸

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.NewTeamSettingUi

-- 将需要操作的UI控件对应到成员
-- 以后改UI控件名字或者层级的时候只需要改此处对应关系即可
function ClassRef:bindUINameToLocalName()
	self._closeBtn = self._ui["Panel/Button_close"]
	self._relocateBtn = self._ui["Panel/Panel_top/Button_3"]

	-- 队伍设置node
	self._teamSettingNode = self._ui["Panel/set"]
	self._teamSettingInfo = NewTeamSettingPanel.create( self._teamSettingNode )
	self._teamSettingInfo:registerOnOperate(function(op, ...)
				executeFunc(self._onOperate, op, ...) -- 交由controller处理
			end)

	self._teamSettingInfo:setTitleShownWay( true )

	-- 队伍目标
	self._target = self._ui["Panel/Panel_top/target"]

	-- 自动跟随节点
	self._autoFollowNode = self._ui["Panel/Panel_top/Node_autoFollow"]
	self._autoFollowSelectedBtn = self._ui["Panel/Panel_top/Node_autoFollow/selected_btn"]

	self._targetBg = self._ui["Panel/Panel_bg/bg04"]
	targetnameBgContentSize = self._targetBg:getContentSize()
	disOFNameAndBg = self._target:getPositionX() - self._targetBg:getPositionX()
end

function ClassRef:init()
	self.isAutoFollowFlag = false -- 是否跟随标记

	self:bindUINameToLocalName()

	-- 关闭UI
	self._closeBtn:addClickEventListener(function()
		executeFunc(self._onCloseView)
	end)

	-- 重定位到地下城队伍搜索器
	self._relocateBtn:addClickEventListener(function()
		executeFunc(self._onOperate, "TeamSetting_relocateToTeamSearching")
	end)

	-- 切换自动跟随状态
	self._autoFollowSelectedBtn:addClickEventListener(function()
		if not self.isAutoFollowFlag then -- 自动跟随
			local creataeTeamDialog = display.newConfirmDialog( L("lua_code_text_429"),
				function() -- 确定
					executeFunc(self._onOperate, "TeamSetting_changeAutoFollowStatus")
					self:setAutoFollowStatus( not self.isAutoFollowFlag )
				end)
			UIHandler:addPopDialog( creataeTeamDialog )
		else
			executeFunc(self._onOperate, "TeamSetting_changeAutoFollowStatus")
			self:setAutoFollowStatus( not self.isAutoFollowFlag )
		end
	end)
end

function ClassRef:show()
	self:setVisible(true)
	self:openAction()
end


function ClassRef:refreshTeamSetting( newTeamVo )
	self._teamSettingInfo:setData( newTeamVo )
end

-- 自动跟随按钮可见性
function ClassRef:showAutoFollow( shown )
	self._autoFollowNode:setVisible( shown )
end

-- 自动跟随按钮状态
function ClassRef:setAutoFollowStatus( isAutoFollow )
	self.isAutoFollowFlag = isAutoFollow
	self._autoFollowSelectedBtn:setBright( isAutoFollow )
end

-- 自动跟随按钮可点击态
function ClassRef:setAutoFollowBtnClickEnabled( clickEnable )
	display.setBtnTouchEnable( self._autoFollowSelectedBtn, clickEnable )
end

-- 设置队伍目标
function ClassRef:setTarget( target )
	if "" == target then
		self._target:setString( L("lua_code_text_55") )
	else
		self._target:setString( target )
	end
	self._targetBg:setContentSize( cc.size( disOFNameAndBg + self._target:getVirtualRendererSize().width + 80, targetnameBgContentSize.height ) )
end

-- 返回修改之后的队伍设置数据
function ClassRef:getTeamRecruitInfoEndingModified()
	return self._teamSettingInfo:getTeamRecruitInfoEndingModified()
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

return ClassRef
